package com.vfa.fishing.animal;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.vfa.fishing.GameActivity;
import com.vfa.fishing.common.AbstractObject;
import com.vfa.fishing.common.Constant;

import android.os.Parcel;

public class Fish extends AbstractObject {

    // Constructor
    public Fish(int x, int y, TiledTextureRegion pTextureRegion, int sideApp, int ikind) {
        super(x, y, pTextureRegion, sideApp);
        kind = ikind;
        sideAppear = sideApp;
        if(sideAppear == Constant.LEFTSIDE){
            direction = Constant.RIGHT;
        }
        else if(sideAppear == Constant.RIGHTSIDE){
            direction = Constant.LEFT;
        }
        // set van toc di chuyen cua ca
        if(kind == 0){
            speedX = GameActivity.rand.nextInt(10)+1;
        }
        else if(kind == 1){
            speedX = GameActivity.rand.nextInt(10)+3;
            speedY = GameActivity.rand.nextInt(10)+3;
        }
        // cap khoi luong cho ca
        if(kind == 0){
            // ca nay co khoi tuong 5kg va gia 5$
            weight = 5;//5
        }
        else if(kind == 1){
            //ca nay co khoi luong 8kg va gia 10$
            weight = 10;
        }
        else if(kind == 2){
            weight = 15;// gia 15$
         }
        else if(kind == 3){
            weight = 20;//gia 18$
        }
        else if(kind == 4){
            weight = 25;//gia 22$
        }
        else if(kind == 5){
            weight = 30;//gia 30$
        }
        else if(kind == 6){
            weight = 35;//gia 40$
            //KINDTERIBLE
        }
        else if(kind == 7){
            weight = 40;//gia 50$
            //KIND TERIBLE
        }
        else if(kind == 8){
            weight = 45;//gia 60$
            //KIND TERIBLE
        }
        iTimeToResetDirect = 0;
        isReset = false;
        this.kindOfObject = Constant.FISH;
    }
    public Fish(int x, int y, TiledTextureRegion pTextureRegion, int ikind,int speedx,int speedy,int direct,int iweight,int timeToreset,boolean isReset){
        super(x, y, pTextureRegion);
        speedX = speedx;
        speedY = speedy;
        direction  = direct;
        weight = iweight;
        this.kindOfObject = Constant.FISH;
    }
    public void update() {
        iTimer = GameActivity.TIMER;
        float X = this.getX();
        float Y = this.getY();
        if(Y<= Constant.WATERLINE || Y>= Constant.BACKGROUND){
            resetDirection();
        }
        if (kind == 0) {// cá này nặng 5kg,giá 5$
              if(!this.checkCollision()) {
                  if (iTimer%20 == 0) {
                      speedX = GameActivity.rand.nextInt(10) + 1;
                      speedY = GameActivity.rand.nextInt(3) + 1;
                  }
                  if (direction == Constant.RIGHT) {
                      X += speedX;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT) {
                      X -= speedX;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.RIGHT_UP) {
                      X += speedX;
                      Y -= speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.RIGHT_DOWN) {
                      X += speedX;
                      Y += speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT_UP) {
                      X -= speedX;
                      Y -= speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT_DOWN) {
                      X -= speedX;
                      Y += speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.UP) {
                      Y -= 15;
                      this.setPosition(X, Y);
                  }
              }
        }
        else if(kind == 1) { // cá này nặng 7kg,giá 9$
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 2) { // cá này nặng 11kg,giá 15$
            if(!this.checkCollision()) {
                if(iTimer%10 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = speedX;
                }
                if(direction == Constant.RIGHT) {
                    direction = Constant.RIGHT_UP;
//                    X += speedX;
//                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
//                    X -= speedX;
//                    Y -= speedX;
                    direction = Constant.LEFT_UP;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 3) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+4;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 4) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 5) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 6) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 7) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 8) {
            if(!this.checkCollision()) {
                if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.UP) {
                    Y -= 15;
                    this.setPosition(X, Y);
                }
            }
        }
    }
    @Override
    public void Eat() {
        // TODO Auto-generated method stub

    }
    @Override
    public void resetDirection() {
        // TODO Auto-generated method stub
//        if(isReset)
//            return;
        if(direction == Constant.RIGHT){

            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect1[randdomDirect];
            isReset = true;

            return;
        }
        else if(direction == Constant.LEFT){

            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect2[randdomDirect];
            isReset = true;

            return;
        }
        else if(direction == Constant.RIGHT_UP){
            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect3[randdomDirect];
            isReset = true;
            return;
        }
        else if(direction == Constant.RIGHT_DOWN){
            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect4[randdomDirect];
            isReset = true;
            return;
        }
        else if(direction == Constant.LEFT_UP){
            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect5[randdomDirect];
            isReset = true;
            return;
        }
        else if(direction == Constant.LEFT_DOWN){
            int randdomDirect = GameActivity.rand.nextInt(2);
            direction = Constant.arrDirect6[randdomDirect];
            isReset = true;
            return;
        }

    }
    @Override
    public int getCoin() {
        // TODO Auto-generated method stub
        return Constant.COIN_FISH_1;
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
}